'use-strict';
//集成 nunjucks 到 app

//引入模块
const nunjucks = require('nunjucks')

function createEnv (path, opts) {
    let
        autoescape = opts.autoescape === undefined ? true : opts.autoescape,
        noCache = opts.noCache || false,
        watch = opts.watch || false,
        throwOnUndefined = opts.throwOnUndefined || false,
        env = new nunjucks.Environment(
            new nunjucks.FileSystemLoader(path || 'views', {
                noCache: noCache,
                watch: watch,
            }), {
                autoescape: autoescape,
                throwOnUndefined: throwOnUndefined
            }
        )
        if (opts.filters) {
            for (var f in opts.filters) {
                env.addFilter(f, opts.filters[f])
            }
        }
        return env
}

// 为ctx 创建 render
function templating(path, opts) {
    // // 创建Nunjucks的env对象:
    // var env = createEnv(path, opts);
    // return async (ctx, next) => {
    //     // 给ctx绑定render函数:
    //     ctx.render = function (view, model) {
    //         // 把render后的内容赋值给response.body:
    //         ctx.response.body = env.render(view, Object.assign({}, ctx.state || {}, model || {}));
    //         // 设置Content-Type:
    //         ctx.response.type = 'text/html';
    //     };
    //     // 继续处理请求:
    //     await next();
    // };
    let env = createEnv(path, opts);
    return async (ctx, next) => {
        ctx.render = function (view, model) {
            ctx.response.body = env.render(view, Object.assign({}, ctx.state || {}, model || {}))
            ctx.response.type = 'text/html'
        }
        await next()
    }
}

module.exports = templating